package com.byteslounge.websockets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import java.io.IOException;

/**
 * @author 311633
 * @version 1.0
 * @ClassName:
 * @Description:
 * @date 2017/4/24 17:27
 */
public class PushServlet extends HttpServlet {

    /**
     * post 方法，接受post请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    /**
     * get 方法，接受get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String empCode = request.getParameter("user");
        System.out.println("用户：" + empCode);
        Session session = SessionCache.get(empCode);

        if (session != null) {
            session.getBasicRemote().sendText("用户：" + empCode + " 的消息");
        }else {
            System.out.println("Session 不存在，不能推送消息。");
        }

        response.getWriter().write("OK");
    }
}
